﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WxPayAPI;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xModel;
using System.Data;
using Yule8xWechat.Applib;
using Yule8xModel.PageModels;

namespace Yule8xWechat.Payment.WxPay
{
    public partial class ResultNotifyPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LogUtil.Instance.WriteInfo("[微信支付回调通知页面-ResultNotifyPage.aspx] 太好了，调用支付回调页面了");
            ResultNotify resultNotify = new ResultNotify(this);
            resultNotify.ProcessNotify(CallFunc);
            Response.Write("success");
            Response.End();
        }

        // 模拟数据
        void CallFunc(WxPayData notifyData)
        {
            string openid = notifyData.GetValue("openid").ToString();
            string attach = notifyData.GetValue("attach").ToString();
            string out_trade_no = notifyData.GetValue("out_trade_no").ToString();

            LogUtil.Instance.WriteInfo(string.Format("[微信支付回调通知页面-ResultNotifyPage.aspx] out_trade_no={0}, openid={1}, attach={2}", out_trade_no, openid, attach));
            string outmsg;
            if (attach.IndexOf("buydiamond:") > -1)
            {
                int rebate_amount = AppConf.AgentRegistReturnAmount;
                decimal parent1_commission_rate = AppConf.Parent1AgentCommissionRate; // 上级是代理, 获得25%返佣
                decimal parent2_commission_rate = AppConf.Parent2AgentCommissionRate; // 上上级是代理, 获得15%返佣

                WxPayResultNotifyPageModels.BuyCardPay(out_trade_no, rebate_amount, parent1_commission_rate, parent2_commission_rate, out outmsg);
                Response.Redirect("../../Agent/Index.aspx", true);
            }
            else if (attach.IndexOf("agentregist:") > -1)
            {
                int number = AppConf.AgentRegistCardCount; // 注册成为代理购买钻石数量
                decimal amount = AppConf.AgentRegistCardAmount; // 注册成为代理付款金额
                int rebate_amount = AppConf.AgentRegistReturnAmount;
                decimal parent1_commission_rate = AppConf.Parent1AgentCommissionRate; // 上级是代理, 获得25%返佣
                decimal parent2_commission_rate = AppConf.Parent2AgentCommissionRate; // 上上级是代理, 获得15%返佣
                WxPayResultNotifyPageModels.AgentRegistPay(out_trade_no, attach, number, amount, rebate_amount, parent1_commission_rate, parent2_commission_rate, out outmsg);
                Response.Redirect("../../Agent/Index.aspx", true);

            }
            else
            {
                LogUtil.Instance.WriteInfo(string.Format("[微信支付回调通知页面-ResultNotifyPage.aspx] attach数据前缀无效！attach={0}", attach));
            }
        }
    }
}